package installer;

import java.util.List;

import org.apache.lucene.document.Document;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.skyline.wizard.dao.DocDao;
import com.skyline.wizard.dao.SeUpdateDao;
import com.skyline.wizard.indexer.Indexer;
import com.skyline.wizard.type.SeDocType;

public class IndexInstaller {
	
	private static Indexer articleIndexer;
	private static Indexer userIndexer;
	private static DocDao docDao;
	private static SeUpdateDao seUpdateDao;

	@BeforeClass
	public static void before() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("config/context/applicationContext.xml");
		articleIndexer = (Indexer) ctx.getBean("articleIndexer");
		userIndexer = (Indexer) ctx.getBean("userIndexer");
		docDao=(DocDao)ctx.getBean("docDao");
		seUpdateDao=(SeUpdateDao)ctx.getBean("seUpdateDao");
	}

	@Test
	public void createArticleIndex() {
		articleIndexer.createIndex();
		List<Document> docs=docDao.queryAllDocument(SeDocType.ARTICLE);
		articleIndexer.addDocuments(docs);
		articleIndexer.optimize();
		
		seUpdateDao.updateLastUpdateTime(SeDocType.ARTICLE);
	}
	
	@Test
	public void createUserIndex() {
		userIndexer.createIndex();
		List<Document> docs=docDao.queryAllDocument(SeDocType.USER);
		userIndexer.addDocuments(docs);
		userIndexer.optimize();
		
		seUpdateDao.updateLastUpdateTime(SeDocType.USER);
	}
}
